﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace Xinnos.Lca.Yacht.Analyzer.View
{
    /// <summary>
    /// Interaction logic for AssessmentPanel.xaml
    /// </summary>
    public partial class ComponentRatePanel : UserControl
    {
        private WorkBreakdownStructuresViewModel mStructures = null;
        private EnvironmentalProfilesViewModel mProfiles = null;
        private DataGridTextColumn mSummaryColumn = null;
        public ComponentRatePanel()
        {
            InitializeComponent();
            mStructures = WorkBreakdownStructuresViewModel.GetInstance();
            mProfiles = EnvironmentalProfilesViewModel.GetInstance();
            mStructures.Groups.CollectionChanged += OnGroupCollectionChanged;
            mProfiles.Profiles.CollectionChanged += OnProfileCollectionChanged;
            Initialize();
        }

        public void Initialize()
        {
            dataGridAssessment.Columns.Clear();
            if (null == mSummaryColumn)
            {
                mSummaryColumn = new DataGridTextColumn();
                Binding _binding = new Binding("Summary");
                mSummaryColumn.Binding = _binding;
                mSummaryColumn.Header = "Summary";
                mSummaryColumn.IsReadOnly = true;
                mSummaryColumn.ElementStyle = FindResource("cellStyle") as Style;
                dataGridAssessment.Columns.Add(mSummaryColumn);
            }
            foreach (WorkBreakdownStructureGroupViewModel _group in mStructures.Groups) AppendGroupColumn(_group);
            foreach (EnvironmentalProfileViewModel _profile in mProfiles.Profiles) AppendProfileColumn(_profile);
            if (1 < dataGridAssessment.Columns.Count) dataGridAssessment.Columns[0].SortDirection = ListSortDirection.Ascending;
        }
        private int GetGroupColumnIndex(WorkBreakdownStructureGroupViewModel group)
        {
            ConfigurationViewModel _configuration = ConfigurationViewModel.GetInstance();
            int _index = mStructures.Groups.IndexOf(group);
            int _reserved = 0;
            foreach (WorkBreakdownStructureGroupViewModel _group in mStructures.Groups)
            {
                if (_configuration.Object == _group.Name) break;
                _reserved++;
            }
            if (_reserved < mStructures.Groups.Count && _reserved < _index) _index--;
            return _index;
        }
        private void AppendGroupColumn(WorkBreakdownStructureGroupViewModel group)
        {
            if (null == group) return;
            ConfigurationViewModel _configuration = ConfigurationViewModel.GetInstance();
            if (group.Name == _configuration.Object) return;
            group.PropertyChanged += OnGroupPropertyChanged;
            int _index = GetGroupColumnIndex(group);
            int _cursor = dataGridAssessment.Columns.IndexOf(mSummaryColumn);
            DataGridTextColumn _column = new DataGridTextColumn();
            _column.Header = group.Name;
            _column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
            Binding _binding = new Binding(string.Format("Sequences[{0}].Name", _index));
            _column.Binding = _binding;
            _column.IsReadOnly = true;
            _column.ElementStyle = FindResource("cellStyle") as Style;
            _column.CanUserSort = true;
            if (_cursor > -1) dataGridAssessment.Columns.Insert(_cursor, _column);
            else dataGridAssessment.Columns.Add(_column);
        }
        private void RemoveGroupColumn(WorkBreakdownStructureGroupViewModel group)
        {
            int _boundary = dataGridAssessment.Columns.IndexOf(mSummaryColumn);
            for (int i = 0; i < _boundary; i++)
            {
                if (group.Name == (dataGridAssessment.Columns[i].Header as string))
                {
                    dataGridAssessment.Columns.RemoveAt(i);
                    break;
                }
            }
        }
        private void AppendProfileColumn(EnvironmentalProfileViewModel profile)
        {
            if (null == profile) return;
            profile.PropertyChanged += OnProfilePropertyChanged;
            DataGridTextColumn _column = new DataGridTextColumn();
            _column.Header = profile.Name;
            _column.Width = new DataGridLength(1, DataGridLengthUnitType.Star);
            Binding _binding = new Binding(string.Format("Factors[{0}]", mProfiles.Profiles.IndexOf(profile)));
            _column.Binding = _binding;
            _column.ElementStyle = FindResource("cellStyle") as Style;
            dataGridAssessment.Columns.Add(_column);
        }
        private void RemoveProfileColumn(EnvironmentalProfileViewModel profile)
        {
            int _start = dataGridAssessment.Columns.IndexOf(mSummaryColumn) + 1;
            if (_start < dataGridAssessment.Columns.Count)
            {
                for (int i = _start; i < dataGridAssessment.Columns.Count; i++)
                {
                    if (profile.Name == (dataGridAssessment.Columns[i].Header as string))
                    {
                        dataGridAssessment.Columns.RemoveAt(i);
                        break;
                    }
                }
            }
        }

        private void OnGroupPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "Name":
                    WorkBreakdownStructureGroupViewModel _group = sender as WorkBreakdownStructureGroupViewModel;
                    int _index = GetGroupColumnIndex(_group);
                    dataGridAssessment.Columns[_index].Header = _group.Name;
                    break;
            }
        }
        private void OnProfilePropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "Name":
                    EnvironmentalProfileViewModel _profile = sender as EnvironmentalProfileViewModel;
                    int _index = mProfiles.Profiles.IndexOf(_profile);
                    int _start = dataGridAssessment.Columns.IndexOf(mSummaryColumn) + 1;
                    dataGridAssessment.Columns[_start + _index].Header = _profile.Name;
                    break;
            }
        }
        private void OnGroupCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    foreach (WorkBreakdownStructureGroupViewModel _group in e.NewItems) AppendGroupColumn(_group);
                    break;
                case NotifyCollectionChangedAction.Remove:
                    foreach (WorkBreakdownStructureGroupViewModel _group in e.OldItems) RemoveGroupColumn(_group);
                    break;
                case NotifyCollectionChangedAction.Reset:
                    Initialize();
                    break;
            }
        }
        private void OnProfileCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    foreach (EnvironmentalProfileViewModel _profile in e.NewItems) AppendProfileColumn(_profile);
                    break;
                case NotifyCollectionChangedAction.Remove:
                    foreach (EnvironmentalProfileViewModel _profile in e.OldItems) RemoveProfileColumn(_profile);
                    break;
                case NotifyCollectionChangedAction.Reset:
                    Initialize();
                    break;
            }
        }
    }
}